//
//  TDUserIcon.m
//  EveryDay
//
//  Created by tidoo on 15/1/28.
//  Copyright (c) 2015年 yuan wei. All rights reserved.
//

#import "JHUserIcon.h"

@implementation JHUserIcon

+ (NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath =[documentsDirectory stringByAppendingPathComponent:@"userIconInfo.plist"];
    return filePath;
}

+ (BOOL)checkFilePathForFile
{
    NSString *filepath = [JHUserIcon filePath];
    NSFileManager *fileManger = [NSFileManager defaultManager];
    if ( [fileManger fileExistsAtPath:filepath] )
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (NSMutableDictionary *)getUserIconFile
{
    NSString *filepath = [JHUserIcon filePath];
    return [NSMutableDictionary dictionaryWithContentsOfFile:filepath];
}

+ (void)saveUserIconInfo:(NSDictionary *)iconDict
{
    NSString *filepath = [JHUserIcon filePath];
    [iconDict writeToFile:filepath atomically:NO];
    [JHUserIcon UserIcon:iconDict];
}

+ (void)removeUserIconInfo
{
    if ([self checkFilePathForFile])
    {
        NSFileManager* fm = [NSFileManager defaultManager];
        [fm removeItemAtPath:[JHUserIcon filePath] error:nil];
    }
}
+ (void)UserIcon:(NSDictionary *)dic
{
    UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"icon"]]]];
    NSData *data = UIImagePNGRepresentation(image);
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userIcon123.png"];
    [data writeToFile:path atomically:YES];
}
+ (NSString *)getUserIcon
{
    return   [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userIcon123.png"];
    
}
@end
